<template>
  <div class="portfolio-similar">
    <div v-if="false">{{testData}}</div>
    <div class="similar-none" v-if="groupList.length == 0">
      <table>
        <tbody>
        <tr>
          <td><img src="~assets/images/portfolio/aiyou.png"></td>
          <td><span>哎哟，这位组合真是策略独特，目前 小的没有发现与他类似的组合呢~</span></td>
        </tr>
        </tbody>
      </table>
    </div>
    <div class="similar-list" v-else>
      <span>好巧不巧正正好，这些组合也有相似的板块持仓</span>
      <div class="similar-list-portfolio" v-for="(group,index) in groupList" v-if="index<5"
           @click.stop.prevent="toSimilarGroup(group)">
        <div>总收益率</div>
        <div class="total_profitrate" v-if="!group.total_floatprofit &&group.total_floatprofit!==0">--%</div>
        <div class="total_profitrate" :class="group.total_floatprofit<0?'loss':'earn'">
          {{group.total_floatprofit.toFixed(2)}}%
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import _ from 'underscore'
  import CommonUtil from 'common/common.util'
  import Constant from 'common/constant'
  import RemoteDAO from 'common/remoteDAO'
  import LocalDAO from 'common/localDAO'

  export default {
    name: 'SimilarGroup',
    data () {
      return {
        error: '',
        testData: "testData",
        groupList: []
      }
    },
    props: {
      clientId: {
        type: String
      },
      groupId: {
        type: String
      }
    },
    created: function () {
      this.fetchData()
    },
    watch: {
      '$route.query.groupId' (to, from) {
        //跳转相同页面，组件不重新构建，无法调用created,故需监听
        this.fetchData ()
      }
    },
    methods: {
      fetchData: function () {
        var self = this;
        RemoteDAO.callJCL30026(self.clientId, self.groupId, function (response) {
          if (Number(response.error) === 0) {
            var groupList = response.data.body.data;
            if (Number(response.data.body.status) == 0) {
//              _.each(groupList, function (group, index) {
//                group.total_floatprofit = CommonUtil.toFixed(group.total_floatprofit, 2)
//              });
              self.groupList = groupList;
            } else {
              self.error = response.data.body.describe;
            }
          } else {
            self.error = "相似组合获取失败";
          }

          //更新分享数据
          var shareGroup = LocalDAO.getShareGroup();
          shareGroup.similar_data = {"groupList":self.groupList};
          LocalDAO.setShareGroup(shareGroup);
        });
      },
      toSimilarGroup(group){
        this.$router.push({path: '/groupdetail', query: {clientId: group.client_id, groupId: group.group_id}});
      }
    },
  }
</script>

<style scoped>

</style>
